package org.example;

//import com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector;

import java.io.*;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

/**
 * Hello world!
 */
public class AppReadSimpleJar {
    public static void main(String[] args) throws Exception {

        HashMap<String, byte[]> map = new HashMap<>();

//        String jarPath = System.getProperty("user.dir") + "/jarFileRear/jarFileDir/simpleJarExample.jar";
        String jarPath = System.getProperty("user.dir") + "/jarFileRear/jarFileDir/boot-01-helloworld-1.0-SNAPSHOT.jar";
        File jarFilePath = new File(jarPath);

        try (JarFile jarFile = new JarFile(jarFilePath)) {

            System.out.println("-----------------------------------------------------------------------------------");
            // 获取 JAR 文件中的所有条目
            jarFile.stream().forEach(entry -> {
                System.out.println("Entry: " + entry.getName());

                // 如果需要读取特定文件
                if (entry.getName().equals("my.properties")) {
                    try (InputStream is = jarFile.getInputStream(entry)) {
                        // 读取文件内容
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                        String line;
                        while ((line = reader.readLine()) != null) {
                            System.out.println(line);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                if (entry.getName().equals("org/example/AppSimpleJar.class")) {

                    try {
                        InputStream inputStream = jarFile.getInputStream(entry);

                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                        byte[] buffer = new byte[1024];
                        int length = -2;
                        while ((length = inputStream.read(buffer)) != -1) {
                            byteArrayOutputStream.write(buffer, 0, length);
                        }

                        byte[] bytes = byteArrayOutputStream.toByteArray();
                        System.out.println(bytes.length);

                        map.put("AppSimpleJar.class", bytes);

                        MyClassLoader myClassLoader = new MyClassLoader(map);

                        Class<?> aClass = myClassLoader.findClass("AppSimpleJar.class");

                        System.out.println(aClass.getClass());

                        aClass.getMethod("main", String[].class).invoke(null, (Object) new String[0]);

                        System.out.println("---------------------------------");

                        String clazzFile = System.getProperty("user.dir") + "/jarFileRear/target/classes/org/example/AppSimpleJar.class";

                        FileOutputStream fileOutputStream = new FileOutputStream(clazzFile);
                        fileOutputStream.write(bytes, 0, bytes.length);
                        fileOutputStream.flush();


                        Class<?> aClass1 = Class.forName("org.example.AppSimpleJar");
                        System.out.println(aClass1);
                        aClass1.getMethod("main", String[].class).invoke(null, (Object) new String[0]);

                        fileOutputStream.close();


                        inputStream.close();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }


                }

            });
        }
    }
}
